# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Testing
# RoleDef/Role and Dependencies
#              Proxy
#              /  |
#           Head  |            
#             \   |
#            Workers (2 of them)
#
# VMs
# VM0: Proxy+Head
# VM1: Worker1
# VM2: Worker2
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

from crv.model import *
from crv.tools.crvtools import buildRoleDep

EC2_URL= "https://ec2.us-east-1.amazonaws.com"
EC2_ACCESS_KEY=''
EC2_SECRET_KEY=''


def createClusterModels(metadata,session):
    (rp,rpprivate, rw1, rw2)=createAnalysisResourcePoolsEC2(metadata,session)
#    (rp,rpprivate, rw1, rw2)=createAnalysisResourcePoolsMagellan(metadata,session)
#    (rp,rpprivate)=createAnalysisResourcePoolsAcs(metadata,session)    
    createAnalysisCluster(metadata,session,rp,rpprivate, rw1, rw2)
    
    (xrp,xrpprivate, xrw1, xrw2)=createXrootdResourcePoolsEC2(metadata,session)
    createXrootdCluster(metadata,session,xrp,xrpprivate, xrw1, xrw2)
    


def createAnalysisResourcePoolsEC2(metadata,session):
    
    #Resource Pool

    magellan_euca=EucaVMMaker("ec2")
    
    magellan_euca.setAttr('EC2_URL', EC2_URL)
    magellan_euca.setAttr('EC2_ACCESS_KEY', EC2_ACCESS_KEY)
    magellan_euca.setAttr('EC2_SECRET_KEY',EC2_SECRET_KEY)
    
    magellan_euca.setAttr('image_id', 'ami-5a944933') # T-Tbar Tests AMI
    magellan_euca.setAttr('keyname', 'cloudcrvkey')
    magellan_euca.setAttr('kernel_id', 'aki-b51cf9dc')
    magellan_euca.setAttr('ramdisk_id', 'ari-b31cf9da')
#    magellan_euca.setAttr('instance_type', 'c1.xlarge' )
#    magellan_euca.setAttr('instance_type', 'm1.large' )
    magellan_euca.setAttr('instance_type', 't1.micro' )
    magellan_euca.setAttr('group_names', 'cloudcrv-worker' )
    magellan_euca.setAttr('addressing_type', "public")

    cpc=CentOS5_Puppet_ClientMaker(name="RHEL5_Puppet",doc="CRVClient for RHEL5 Clients using Puppet",templatedir="/opt/cloudcrv/cloudcrv/crvclient")
    ppm=Puppet_ProfileMaker(name="Puppet",doc="Profile Maker using Puppet")

    rp=RP("publicIP","Resource Pool with Publicly Addressed VMs, Puppet Profiles and RHEL5 Clients",profilemaker=ppm,vmmaker=magellan_euca,clientmaker=cpc)

    session.add(rp)

    return rp, rp, rp, rp


def createAnalysisCluster(metadata,session,rp,rpprivate, rpprivate_worker1,rpprivate_worker2):

    #Role Definitions
    rdLDAPSrv=RoleDef('LDAPSrv',"LDAP Server","at3_ldapserver","puppet")
    
    rdLDAPClient=RoleDef('LDAPClient',"LDAP Linux Auth Client","at3_ldapclient","puppet")
    rdLDAPClient.addDependOn(rdLDAPSrv)
    
    rdNFSSrv=RoleDef('NFSSrv',"NFS Server","at3_nfssrv","puppet")
    
    
    rdNFSClient=RoleDef('NFSClient',"NFS Client","at3_nfsclient","puppet")
    rdNFSClient.addDependOn(rdNFSSrv)

    rdProxy=RoleDef('Proxy',"Proxy Server","at3_proxy","puppet")
#    rdProxy.addDependOn(rdNFSClient,local=True)
    
    rdCVMFSClient=RoleDef('CVMFSClient',"CVMFS Client","at3_cvmfs","puppet")
    rdCVMFSClient.addDependOn(rdProxy)

    rdHead=RoleDef('Head',"Condor Head","at3_condor","puppet")
    rdHead.addDependOn(rdLDAPClient,local=True)
    rdHead.addDependOn(rdNFSClient,local=True)
    rdHead.addDependOn(rdProxy)

    rdInteractive=RoleDef('Interactive',"Condor Interactive","at3_condor","puppet")
    rdInteractive.addDependOn(rdLDAPClient,local=True)    
    rdInteractive.addDependOn(rdNFSClient,local=True)
    rdInteractive.addDependOn(rdCVMFSClient,local=True)
    rdInteractive.addDependOn(rdHead)

    rdWorker=RoleDef('Worker',"Condor Worker","at3_condor","puppet")
    rdWorker.addDependOn(rdLDAPClient,local=True)    
    rdWorker.addDependOn(rdNFSClient,local=True)    
    rdWorker.addDependOn(rdCVMFSClient,local=True)
    rdWorker.addDependOn(rdHead)
    
    cl=Cluster("tier3 analysis cluster")
    cl.setAttr("confdir","/opt/cloudcrv/puppet")


    rLDAPSrv=cl.addRole(name="LDAP", roledef=rdLDAPSrv, vm=rpprivate.newVM("LDAP Server"),enabled=True)
    rLDAPSrv.setAttr("rootpw","{SSHA}Rq03UQ2HtDnqozwigoXEESAaqMaXq5cr")
    rLDAPSrv.setAttr("suffix","dc=mytier3,dc=com")
    
    
    rNFSSrv=cl.addRole(name="NFS", roledef=rdNFSSrv, vm=rpprivate.newVM("NFS Server"), enabled=True)
    rNFSSrv.setAttr("NFSServerSharePath","/mnt/export/home")
    rNFSSrv.setAttr("NFSServerClientMountPath","/export/home")


    rProxy=cl.addRole(name="Proxy", roledef=rdProxy, vm=rpprivate.newVM("Squid Proxy"), enabled=True)

    rHead=cl.addRole(name="Head",roledef=rdHead,vm=rpprivate.newVM("Condor Head"),enabled=True)
    rHead.setAttr("CondorRoleName","head")
    rHead.setAttr("CondorAllowNegotiatorExtra","")
    rHead.setAttr("CondorFilesystemDomain","atlastier3")
    rHead.setAttr("CondorPassword","abcdefg")
    
    for i in range(1):
        rr=cl.addRole(name="Interactive%02d"%i,roledef=rdInteractive,vm=rp.newVM("Interactive%02d"%i),enabled=True)
        rr.setAttr("CondorRoleName","interactive")
        rr.setAttr("CondorAllowNegotiatorExtra","")
        rr.setAttr("CondorFilesystemDomain","atlastier3")
        rr.setAttr("CondorPassword","abcdefg")

    nWorkers=1

    for i in range(nWorkers):
        if i%2 == 0:
            rr=cl.addRole(name="Worker%03d"%i, roledef=rdWorker, vm=rpprivate_worker1.newVM("Worker%03d"%i), enabled=True)
        else:
            rr=cl.addRole(name="Worker%03d"%i, roledef=rdWorker, vm=rpprivate_worker2.newVM("Worker%03d"%i), enabled=True)

        rr.setAttr("CondorRoleName","worker")
        rr.setAttr("CondorAllowNegotiatorExtra","")
        rr.setAttr("CondorFilesystemDomain","atlastier3")
        rr.setAttr("CondorPassword","abcdefg")               
    
    cl.targetSC=Cluster.SC.SC_INITIALIZED

    buildRoleDep(cl.role)

    session.add(cl)
    
def createXrootdCluster(metadata,session,rp,rpprivate, rpprivate_worker1,rpprivate_worker2):
    #Role Definitions
    rdXrootdRedirector=RoleDef('XROOTDRedirector',"Xrootd Redirector","at3_xrootd_redir","puppet")
    
    rdXrootdStorageNode=RoleDef('XROOTDStorageNode',"XROOTD Storage Node","at3_xrootd_node","puppet")
    rdXrootdStorageNode.addDependOn(rdXrootdRedirector)
    
    cl=Cluster("tier3 xrootd storage")
    cl.setAttr("confdir","/opt/cloudcrv/puppet")


    rXrootdRedirector=cl.addRole(name="XRDRedir", roledef=rdXrootdRedirector, vm=rpprivate.newVM("vmXROOTDRedir"), enabled=True)
    rXrootdRedirector.setAttr("XROOTDInventory","/atlas/inventory")
    rXrootdRedirector.setAttr("XROOTDOssLocalRoot","/local/xrootd/a")
    rXrootdRedirector.setAttr("XROOTDStoragePath","/atlas")
    rXrootdRedirector.setAttr("XROOTDFuse","false")
    rXrootdRedirector.setAttr("XROOTDMountPoint","/xrootdfs/atlas")
    
    for i in range(3):
        rr=cl.addRole(name="XRDStorage%02d"%i,roledef=rdXrootdStorageNode,vm=rp.newVM("vmXROOTDStorage%02d"%i),enabled=True)
        rr.setAttr("XROOTDInventory","/atlas/inventory")
        rr.setAttr("XROOTDOssLocalRoot","/local/xrootd/a")
        rr.setAttr("XROOTDStoragePath","/atlas")
       
    buildRoleDep(cl.role)

    session.add(cl)
    
def createXrootdResourcePoolsEC2(metadata,session):
    
    #Resource Pool

    magellan_euca=EucaVMMaker("ec2")
    
    magellan_euca.setAttr('EC2_URL', EC2_URL)
    magellan_euca.setAttr('EC2_ACCESS_KEY', EC2_ACCESS_KEY)
    magellan_euca.setAttr('EC2_SECRET_KEY',EC2_SECRET_KEY)
    
    #magellan_euca.setAttr('image_id', 'ami-c41ac9ad')
    magellan_euca.setAttr('image_id', 'ami-5034e739')
    magellan_euca.setAttr('keyname', 'cloudcrvkey')
    magellan_euca.setAttr('kernel_id', 'aki-b51cf9dc')
    magellan_euca.setAttr('ramdisk_id', 'ari-b31cf9da')
    #magellan_euca.setAttr('instance_type', 'c1.xlarge' )
    magellan_euca.setAttr('instance_type', 't1.micro' )
    magellan_euca.setAttr('group_names', 'cloudcrv-worker' )
    magellan_euca.setAttr('addressing_type', "public")

    cpc=CentOS5_Puppet_ClientMaker(name="RHEL5_Puppet",doc="CRVClient for RHEL5 Clients using Puppet",templatedir="/opt/cloudcrv/cloudcrv/crvclient")
    ppm=Puppet_ProfileMaker(name="Puppet",doc="Profile Maker using Puppet")

    rp=RP("publicIP","Resource Pool with Publicly Addressed VMs, Puppet Profiles and RHEL5 Clients",profilemaker=ppm,vmmaker=magellan_euca,clientmaker=cpc)

    session.add(rp)

    return rp, rp, rp, rp

    
